package com.jingtai.shop.service.goods.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jingtai.common.enums.DeleteFlagEnum;
import com.jingtai.common.enums.EnableStatusEnum;
import com.jingtai.common.enums.LevelEnum;
import com.jingtai.core.entity.shop.GoodsSpecificationsCustom;
import com.jingtai.core.entity.shop.MerchantsGoodsWarehouse;
import com.jingtai.core.mapper.shop.GoodsSpecificationsCustomMapper;
import com.jingtai.shop.service.goods.GoodsSpecificationsCustomService;
import com.jingtai.shop.service.goods.MerchantsGoodsWarehouseService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商户商品属性表 服务实现类
 * </p>
 *
 * @author GR123
 * @since 2020-11-29
 */
@Service
public class GoodsSpecificationsCustomServiceImpl extends ServiceImpl<GoodsSpecificationsCustomMapper, GoodsSpecificationsCustom> implements GoodsSpecificationsCustomService {

    @Resource
    private GoodsSpecificationsCustomService goodsSpecificationsCustomService;
    @Resource
    private MerchantsGoodsWarehouseService merchantsGoodsWarehouseService;

    @Override
    public List<JSONObject> queryGoodsCustom(String goodsCode) {
        //查询商品
        List<MerchantsGoodsWarehouse> list1 = merchantsGoodsWarehouseService.list(Wrappers.lambdaQuery(MerchantsGoodsWarehouse.class).eq(MerchantsGoodsWarehouse::getGoodsCode, goodsCode).eq(MerchantsGoodsWarehouse::getDeleteFlag, DeleteFlagEnum.NOT_DELETE.getCode()));
        Map<String,String> map = new HashMap<>();
        for (MerchantsGoodsWarehouse merchantsGoodsWarehouse : list1){
            map.put(merchantsGoodsWarehouse.getSpecificationsCode(),merchantsGoodsWarehouse.getSpecificationsCode());
        }
        //1.查询所有一级自定义规格
        List<GoodsSpecificationsCustom> list = goodsSpecificationsCustomService.list(Wrappers.lambdaQuery(GoodsSpecificationsCustom.class).eq(GoodsSpecificationsCustom::getGoodsCode, goodsCode).eq(GoodsSpecificationsCustom::getLevel, LevelEnum.ONELEVEL.getValue()).eq(GoodsSpecificationsCustom::getEnableState, EnableStatusEnum.NORMAL).eq(GoodsSpecificationsCustom::getDeleteFlag, DeleteFlagEnum.NOT_DELETE));
        //2.根据一级自营一规格拆穿你二级
        List<JSONObject> jsonObjectList = new ArrayList<>();
        for (GoodsSpecificationsCustom goodsSpecificationsCustom : list) {
            List<GoodsSpecificationsCustom> list2 = goodsSpecificationsCustomService.list(Wrappers.lambdaQuery(GoodsSpecificationsCustom.class).eq(GoodsSpecificationsCustom::getGoodsCode, goodsCode).eq(GoodsSpecificationsCustom::getLevel, LevelEnum.TWOLEVEL.getValue()).eq(GoodsSpecificationsCustom::getLevelOfOne, goodsSpecificationsCustom.getId()).eq(GoodsSpecificationsCustom::getEnableState, EnableStatusEnum.NORMAL).eq(GoodsSpecificationsCustom::getDeleteFlag, DeleteFlagEnum.NOT_DELETE));
            JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(goodsSpecificationsCustom));
            jsonObject.put("list", list2);
            jsonObjectList.add(jsonObject);
        }


        //2.根据商品编码查询一级自定义规格
        List<GoodsSpecificationsCustom> listOfOne = goodsSpecificationsCustomService.list(Wrappers.lambdaQuery(GoodsSpecificationsCustom.class).eq(GoodsSpecificationsCustom::getGoodsCode, goodsCode).eq(GoodsSpecificationsCustom::getLevel, LevelEnum.ONELEVEL.getValue()).eq(GoodsSpecificationsCustom::getEnableState, EnableStatusEnum.NORMAL.getValue()).eq(GoodsSpecificationsCustom::getDeleteFlag, DeleteFlagEnum.NOT_DELETE.getCode()));
        //3.根据一级自定义规格 查询二级自定义规格
        List<List<String>> lists = new ArrayList<>();
        for (GoodsSpecificationsCustom goodsSpecificationsCustom : listOfOne){
            List<GoodsSpecificationsCustom> listOfTwo = goodsSpecificationsCustomService.list(Wrappers.lambdaQuery(GoodsSpecificationsCustom.class).eq(GoodsSpecificationsCustom::getGoodsCode, goodsCode).eq(GoodsSpecificationsCustom::getLevelOfOne,goodsSpecificationsCustom.getId()).eq(GoodsSpecificationsCustom::getLevel, LevelEnum.TWOLEVEL.getValue()).eq(GoodsSpecificationsCustom::getEnableState, EnableStatusEnum.NORMAL.getValue()).eq(GoodsSpecificationsCustom::getDeleteFlag, DeleteFlagEnum.NOT_DELETE.getCode()));
            List<String> stringList = new ArrayList<>();
            for (GoodsSpecificationsCustom goodsSpecificationsCustom1 : listOfTwo){
                stringList.add(goodsSpecificationsCustom1.getSpecificationsName());
            }
            lists.add(stringList);
        }
        return jsonObjectList;
    }
}
